<template>
  <el-card class="userinfo-container">
    <!-- 面板屑导航 -->
    <bread first="个人中心" second="重置密码"></bread>
    <el-form
      :model="resetForm"
      :rules="infoRuels"
      ref="infoRef"
      label-width="100px"
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="resetForm.old_pwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="resetForm.new_pwd"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="resetForm.re_pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatePwd">修改密码</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
           
<script>
import { updatePwdApi } from '@/api/user'
export default {
  name: '',
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('输入不能为空,请再次输入密码'))
      } else if (value !== this.resetForm.new_pwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 表单信息对象
      resetForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: '',
      },
      // 验证规则对象
      infoRuels: {
        old_pwd: [
          {
            required: true,
            message: '旧密码不能为空',
            trigger: ['blur', 'blur'],
          },
        ],
        new_pwd: [
          {
            required: true,
            message: '新密码不能为空',
            trigger: ['blur', 'blur'],
          },
        ],
        re_pwd: [
          {
            required: true,
            message: '请在此输入密码',
            trigger: ['blur', 'blur'],
          },
          { validator: validatePass2, trigger: 'blur' },
        ],
      },
    }
  },
  created() {},
  computed: {},
  methods: {
    updatePwd() {
      /* 
      1、获取表单值
      2、发送请求
      3、成功后跳转回登录页面
       */
      this.$refs.infoRef.validate(async (valid) => {
        if (valid) {
          const res = await updatePwdApi(this.resetForm)
          console.log(res, 44)
          if (res.data.code === 0) {
            this.$router.push('/login')
          }
        }
      })
    },
  },
}
</script>
<style lang='less'  scoped>
.el-form {
  width: 50%;
  margin: 30px 0;
}
</style>